package com.zhiche.lisa.mdm.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.zhiche.lisa.mdm.dao.mapper.ServiceTypeMapper;
import com.zhiche.lisa.mdm.dao.model.ServiceType;
import com.zhiche.lisa.mdm.dao.model.TransportationType;
import com.zhiche.lisa.mdm.dto.LisaFeeReceivableDTO;
import com.zhiche.lisa.mdm.service.IServiceTypeService;
import com.zhiche.lisa.mdm.service.ITransportationTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * <p>
 * 业务类型 服务实现类
 * </p>
 *
 * @author qichao
 * @since 2018-06-18
 */
@Service
public class ServiceTypeServiceImpl extends ServiceImpl<ServiceTypeMapper, ServiceType> implements IServiceTypeService {
    @Autowired
    private ITransportationTypeService iTransportationTypeService;

    @Override
    public List<ServiceType> getServiceType(String parentCode) {
        EntityWrapper<ServiceType> ew = new EntityWrapper<>();
        ew.eq("parent_code",parentCode);
        return this.selectList(ew);
    }

    @Override
    public List<ServiceType> getServiceTypeByCodes(String codes) {
        List<String> list = Arrays.asList(codes.split(";"));
        List<ServiceType> serviceTypes = baseMapper.getServiceTypeByCode(list);
        return  serviceTypes;
    }
    /**
     * 根据 ids 获取运输方式
     */
    @Override
    public Map<Long,String> queryTransModel(Map<String, LisaFeeReceivableDTO> map){
        Map<Long,String> maps = Maps.newHashMap();
        List<Long> transTypeIds = new ArrayList<>();
        for(Map.Entry<String,LisaFeeReceivableDTO> entry : map.entrySet() ){
            if(!ObjectUtils.isEmpty(entry.getValue())){
                transTypeIds.add(entry.getValue().getTransTypeId());
            }
        }
        if(!CollectionUtils.isEmpty(transTypeIds)) {
            List<TransportationType> transportationTypes = iTransportationTypeService.selectBatchIds(transTypeIds);
            if(!CollectionUtils.isEmpty(transportationTypes)) {
                for (TransportationType transportationType : transportationTypes) {
                    if (!ObjectUtils.isEmpty(transportationType)) {
                        EntityWrapper<ServiceType> ew = new EntityWrapper<>();
                        ew.eq("code", transportationType.getTosCode());
                        List<ServiceType> serviceTypes = this.selectList(ew);
                        if(!CollectionUtils.isEmpty(serviceTypes)) {
                            maps.put(transportationType.getId(), transModelReplace(serviceTypes.get(0).getValue()));
                        }
                    }
                }
            }
        }
        return maps;
    }
    /**
     * 运输方式全称转换简称
     *
     */
  private String transModelReplace(String serviceType){
      if(!StringUtils.isEmpty(serviceType)){
          if (serviceType.contains("板车运输")) {
              serviceType = serviceType.replaceAll("板车运输", "公路");
          }
          if (serviceType.contains("铁路运输")) {
              serviceType = serviceType.replaceAll("铁路运输", "铁运");
          }
          if (serviceType.contains("人送地跑")) {
              serviceType = serviceType.replaceAll("人送地跑", "人送");
          }
          if (serviceType.contains("船运")) {
              serviceType = serviceType.replaceAll("船运", "水运");
          }
  }
  return serviceType;
  }
//    /**
//     * 根据运输方式id获取运输方式
//     * @param id
//     * @return
//     */
//    @Override
//    public String getTransModelById(Long id){
//        ServiceType serviceType = new ServiceType();
//        String a = "";
//        if(Objects.nonNull(id)){
//             serviceType = this.selectById(id);
//        }
//        if(Objects.nonNull(serviceType)){
//            a = serviceType.getValue();
//        }
//        return  a;
//    }

}
